<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='批量生成',active='cdkeybatch'">
<header th:replace="header::headerFragment(${title},${active})"></header>
<body class="fixed-left">
<style type="text/css">
</style>
<div id="wrapper">
    <div th:replace="header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <form class="form-horizontal center-block" role="form">
                            <div class="form-group ">
                                <label class="col-sm-2 control-label">管理员账号 </label>
                                <div class="col-sm-6 " id="usernameInput">
                                    <input class="form-control" type="text" name="username" placeholder="管理员账号"/>
                                </div>
                                <div class="col-sm-1 ">
                                    <button id="addUserName" type="button" class="btn btn-success pull-right">
                                        增加租户
                                    </button>
                                </div>
                            </div>
                            <div class="form-group ">
                                <label class="col-sm-2 control-label" for="userCount">用户限制数量 </label>
                                <div class="col-sm-6 ">
                                    <input class="form-control" type="number" value=30
                                           id="userCount"
                                           name="userCount"
                                           placeholder="用户限制数量"/>
                                </div>
                            </div>
                            <div class="form-group ">
                                <label class="col-sm-2 control-label" for="businessCount">业务提交次数 </label>
                                <div class="col-sm-6 ">
                                    <input class="form-control" type="number" value=25
                                           id="businessCount" name="businessCount"
                                           placeholder="业务提交次数"/>
                                </div>
                            </div>
                            <div class="form-group ">
                                <label class="col-sm-2 control-label" for="assetsCount">资产提交数量 </label>
                                <div class="col-sm-6 ">
                                    <input class="form-control" type="number" value=30
                                           id="assetsCount" name="assetsCount"
                                           placeholder="资产提交数量"/>
                                </div>
                            </div>
                            <div class="form-group ">
                                <label class="col-sm-2 control-label" for="daysUsed">使用时长 </label>
                                <div class="col-sm-6 ">
                                    <input class="form-control" type="number" value=30
                                           id="daysUsed"
                                           name="daysUsed"
                                           placeholder="使用时长"/>
                                </div>
                            </div>
                            <div class="form-group ">
                                <label class="col-sm-2 control-label" for="menuCodes">选择服务 </label>
                                <div class="col-sm-6 " id="menuCodes">
                                    <div>
                                        <label>
                                            <input type="checkbox" checked name="selectAll"> 全部服务
                                        </label>
                                    </div>
                                    <div>
                                        <label>
                                            <input type="checkbox" checked name="menuCodes" value="1"> 业务影响分析服务
                                        </label>
                                    </div>
                                    <div>
                                        <label>
                                            <input type="checkbox" checked name="menuCodes" value="2"> 风险分析服务
                                        </label>
                                    </div>
                                    <div>
                                        <label>
                                            <input type="checkbox" checked name="menuCodes" value="3"> 预案管理服务
                                        </label>
                                    </div>
                                    <div>
                                        <label>
                                            <input type="checkbox" checked name="menuCodes" value="4"> 演练服务
                                        </label>
                                    </div>
                                    <div>
                                        <label>
                                            <input type="checkbox" checked name="menuCodes" value="5"> 应急服务
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group ">
                                <label class="col-sm-2 control-label" for="useScreen">是否使用大屏 </label>
                                <div class="col-sm-6">
                                    <select name="useScreen" id="useScreen" class="form-control">
                                        <option value="0">是</option>
                                        <option value="1">否</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group ">
                                <div class="col-sm-4">
                                </div>
                                <div class="col-sm-1">
                                    <button id="generateCode" type="button" class="btn btn-inverse pull-right">批量下载
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div th:replace="footer :: footer-content"></div>
                </div>
            </div>
        </div>
    </div>
    <div th:replace="footer :: footer"></div>
</div>
<script>

    $("#addUserName").click(function () {
        $("#usernameInput").append('<input class="form-control" type="text" style="margin-top: 2px;" name="username" placeholder="管理员账号"/>');
    });
    $("body").on("click", "#generateCode", function () {
        let ajaxURL = "/cd/key/batch/generate";
        let menuCodes = [];
        let usernames = [];
        //意思是选择被选中的checkbox
        $.each($('#menuCodes input:checkbox:checked'), function () {
            menuCodes.push($(this).val());
        });
        $.each($('#usernameInput input[name=username]'), function () {
            usernames.push($(this).val());
        });
        let data = {
            'usernames': usernames,
            'userCount': $('#userCount').val(),
            'businessCount': $('#businessCount').val(),
            'assetsCount': $('#assetsCount').val(),
            'daysUsed': $('#daysUsed').val(),
            'menuCodes': menuCodes,
            'useScreen': $('#useScreen').val(),
            'newAdd': 1
        };
        layer.load(2);
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: ajaxURL,
            data: JSON.stringify(data),
            success: function (res) {
                if (res != null) {
                    if (res.code != null && res.code == 500) {
                        layer.closeAll('loading');
                        layer.alert(res.msg, {
                            icon: 2,
                            skin: 'layer-ext-moon'
                        })
                    } else {
                        layer.closeAll('loading');
                        layer.msg('激活码生成成功');
                        window.location.href = '/profile/激活码.zip';
                    }
                }
            },
            error: function (data) {
                layer.closeAll('loading');
            }
        });
    });

    $("input[type=number]").blur(function () {
        $(this).val($(this).val() > 0 ? $(this).val() : 0);
    });

    $("#download").click(function () {
        exportRaw($('#username').val() + "激活码.txt", $("#showLicenseCode").text());
    });

    $('input[name="selectAll"]').on("click", function () {
        if ($(this).is(':checked')) {
            $('input[name="menuCodes"]').each(function () {
                $(this).prop("checked", true);
            });
        } else {
            $('input[name="menuCodes"]').each(function () {
                $(this).prop("checked", false);
            });
        }
    });
</script>
</body>
</html>